
import { _decorator, director, Component, Node, Enum, resources, AudioSource, AudioClip } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Common')
export class Common  extends Component {

    static State = Enum({
        IDLE: 1,
        ATTACK: 2,
        MOVE: 3,
        DODGE: 4
    })

    static boundary = 3000
    static score = 0
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;


    static audioSource = Enum({

        BG: "bg",
        Attack: "attack",
        flash: "flash",
        die_hero: "die_hero",
        die_enemy: "die_enemy",
        hit_enemy: "hit_enemy",
        enemy_init: "enemy_init"
    })

    start() {
        // [3]



    }
    reStart() {
        console.log("start")

        localStorage.clear()
        Common.setScore(0);

        director.loadScene("1")

    }

    // 碰撞检测。返回true则碰撞;o为hero，n为enemy
    static checkCollide(o, n, long = 0) {
        // 玩家在怪物右边,没有碰撞：玩家的x-玩家的半径>怪物的x+怪物半径；玩家在左边；在上面；在下面

        if (o == undefined || n == undefined)
            return false;

        // 敌人不为空
        let rt = !(!n || o.getPosition().x - o.cW.x - long > n.getPosition().x + n.cW.x || o.getPosition().x + o.cW.x + long < n.getPosition().x - n.cW.x || o.getPosition().y > n.getPosition().y + n.cW.y || o.getPosition().y + o.cW.y < n.getPosition().y);
        // console.log(rt)
        return rt;
    }


    static setScore(v) {
        localStorage.setItem("score", v)
    }

   

    static audio = new AudioSource();
    // 播放音乐。默认播放音效
    static playAudio(name, isMusic = true,vol=0.5) {
        let audio = this.audio;
        const path = "audio/" + name;
        resources.load(path, (err: any, clip: AudioClip) => {
            if (err) {
                console.warn(err)
                return;
            }

            // 音效
            if (isMusic) {
                audio.playOneShot(clip,vol)
            }
            // 背景音乐
            else {
                audio.loop = true;
                audio.clip = clip;
                audio.volume=vol;
                audio.play()
            }

        })
    }


}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
